﻿using Desktop.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Desktop.Controls
{
    public class ActivityBase : UserControl, IActivity, INotifyPropertyChanged
    {
        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        public static readonly RoutedEvent ClosingEvent =
            EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble,
                typeof(RoutedEventHandler), typeof(ActivityBase));

        public static readonly RoutedEvent PrincipalChangedEvent =
            EventManager.RegisterRoutedEvent("PrincipalChanged", RoutingStrategy.Bubble,
                typeof(RoutedEventHandler), typeof(ActivityBase));

        public event RoutedEventHandler Closing
        {
            add
            {
                AddHandler(ClosingEvent, value);
            }
            remove
            {
                RemoveHandler(ClosingEvent, value);
            }
        }

        public event RoutedEventHandler PrincipalChanged
        {
            add
            {
                AddHandler(PrincipalChangedEvent, value);
            }
            remove
            {
                RemoveHandler(PrincipalChangedEvent, value);
            }
        }

        #endregion

        #region Constants

        private const string DEFAULT_NAME = "Activity";

        #endregion

        #region Variables

        private IWorkspace _parent;
        private bool _isCloseable;

        #endregion

        #region Constructors

        public ActivityBase(IWorkspace parent)
        {
            ActivityName = DEFAULT_NAME;
            IsCloseable = true;
            Parent = parent;
            Background = new SolidColorBrush(SystemColors.ControlColor);

            AddHandler(ActivityBase.LoadedEvent, new RoutedEventHandler(OnLoaded));
        }

        public ActivityBase()
            : this(null)
        {
        }

        #endregion

        #region Properties

        public string ActivityName
        {
            get
            {
                return (string)GetValue(ActivityNameProperty);
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    value = DEFAULT_NAME;
                }
                if (ActivityName != value)
                {
                    SetValue(ActivityNameProperty, value);
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("ActivityName"));
                    }
                }
            }
        }

        public static readonly DependencyProperty ActivityNameProperty =
            DependencyProperty.Register("ActivityName", typeof(string), typeof(ActivityBase), new PropertyMetadata(string.Empty));

        public bool IsCloseable
        {
            get
            {
                return _isCloseable;
            }
            set
            {
                if (_isCloseable != value)
                {
                    _isCloseable = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("IsCloseable"));
                    }
                }
            }
        }

        public new IWorkspace Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
            }
        }

        private Button CloseButton { get; set; }

        #endregion

        #region Methods

        public void Close()
        {
            Parent.CloseActivity(this);
        }

        #endregion

        #region Event Handlers

        private void OnLoaded(object sender, EventArgs e)
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                TabControl parent = Parent as TabControl;
                ContentPresenter cp = parent.Template.FindName("PART_SelectedContentHost", parent) as ContentPresenter;
                CloseButton = cp.ContentTemplate.FindName("closeButton", cp) as Button;
                CloseButton.Click += new RoutedEventHandler(CloseButton_Click);
            }
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(WorkspaceTabItem.CloseTabEvent, this));
        }

        #endregion
    }
}